package com.vv.tools.modules.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vv.tools.common.message.Result;
import com.vv.tools.common.validator.AddGroup;
import com.vv.tools.common.validator.UpdateGroup;
import com.vv.tools.modules.sys.converter.RoleConvert;
import com.vv.tools.modules.sys.model.RoleDTO;
import com.vv.tools.modules.sys.service.RoleService;

import jakarta.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author vv
 * @since 2025-02-19
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {


    @Autowired
    private RoleService roleService;

    @Operation(tags = "查询角色", description = "查询角色")
    @GetMapping("/{id}")
    public Result<RoleDTO> query(@PathVariable @NotNull(message = "ID不能为空") Long id) {
        return Result.success(RoleConvert.INSTANCE.do2dto(roleService.getById(id)));
    }

    @Operation(tags = "新增角色", description = "新增角色")
    @PostMapping
    public Result<Boolean> save(@RequestBody @Validated(value = {AddGroup.class}) RoleDTO dto) {
        return Result.success(roleService.addRole(dto));
    }

    @Operation(tags = "删除角色", description = "删除角色")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable @NotNull(message = "ID不能为空") Long id) {
        return Result.success(roleService.deleteRole(id));
    }

    @Operation(tags = "修改角色", description = "修改角色")
    @PutMapping
    public Result<Boolean> update(@RequestBody @Validated(value = {UpdateGroup.class}) RoleDTO dto) {
        return Result.success(roleService.updateRole(dto));
    }

    @Operation(tags = "用户列表", description = "用户列表")
    @GetMapping("/list")
    public Result<Page<RoleDTO>> list(RoleDTO roleDTO) {
        return Result.success(roleService.page(roleDTO));
    }

}
